package com.qf.contract.controller;

import com.qf.common.base.exception.ControllerException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FAddressBook;
import com.qf.contract.service.FAddressBookService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "通讯录的增删改查")
@RestController
@RequestMapping("/faddressbookcontroller")
public class FAddressBookController {
    @Resource
    private FAddressBookService fAddressBookService;

    @ApiOperation("新增通讯录信息")
    @PutMapping("/addfaddressbookservice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "通讯录附件,请上传文件"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addFAddressBookService(FAddressBook fAddressBook) {
        try {
            return RespResult.success(fAddressBookService.addFAddressBookService(fAddressBook));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("查询通讯录信息")
    @GetMapping("/selectfaddressbookservice")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult selectFAddressBookService(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        //设置默认时间
        try {
            //给个时间
            return RespResult.success(fAddressBookService.selectFAddressBookService(current, size));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("删除通讯录信息")
    @DeleteMapping("/delfaddressbookservice")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult delFAddressBookService(int id) {
        try {
            return RespResult.success(fAddressBookService.delFAddressBookService(id));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("修改通讯录信息")
    @PutMapping("/upfaddressbookservice")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult upFAddressBookService(FAddressBook fAddressBook) {
        try {
            return RespResult.success(fAddressBookService.upFAddressBookService(fAddressBook));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }
}
